var request = require('request');
var iconv = require('iconv-lite');

var quanju="8";
request('http://192.168.1.1', function (error, response,body) {
  if (!error&&response.statusCode == 200) {
    //console.log(body) // 请求成功的处理逻辑

    var regExp=/document.getElementById\("Frm_Logintoken"\).value = "(.*)";/;
    var regExp1=/document.getElementById\("Frm_Loginchecktoken"\).value = (\d+);/;
    var res = regExp.exec(body);
    var res1 = regExp1.exec(body);
    //console.log(res[1]);
    //console.log(res1[1]);
    var hh="Frm_Logintoken=" +res[1] + "&Frm_Loginchecktoken=" + res1[1] + "&_cu_url=0&Right=2&Username=&UserRandomNum=95670920&Password=0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f&action=login";

    //console.log(hh);

///第三部分
    //var request = require('request');
request({url:"http://192.168.1.1",
        method:"POST",
        headers:{
            'Cookie': '_TESTCOOKIESUPPORT=1'
        },
        body: hh

    },function (error, response,body) {
  if (!error&&response.statusCode == 200) {
    //console.log(response.headers['set-cookie'][0]) // 请求成功的处理逻辑
    var sid=response.headers['set-cookie'][0].split(';')[0];
    //console.log(sid);

    request({url:'http://192.168.1.1/getpage.gch?pid=1002&nextpage=status_lan_host_info_BJ_t.gch',
        method:'GET',
        headers:{'Cookie':sid}

}, function (error, response,body) {
    if (!error&&response.statusCode == 200) {
        //console.log(body);
        //console.log(typeof body);
        //console.log(iconv.decode(body,'gbk'));

        var regExp3=/Transfer_meaning\('HostName\d+','(.*)'\);/g;
        var regExp4=/Transfer_meaning\('IPAddress\d+','(.*)'\);/g;
        var regExp5=/Transfer_meaning\('MACAddress\d+','(.*)'\);/g;
        var regExp6=/\\x\d[a-z]/g;
        var host='"';
        var ip='"';
        var mac='"';
        while(res=regExp3.exec(body)){

            //console.log(res[1]);
            var uu=res[1].replace(regExp6,'-');
            if (uu=='') {uu="None"}
            //console.log(uu);
            //host.push(uu);
            host=host+uu+'","';
        }

        //console.log(host);
        while(res=regExp4.exec(body)){
            //console.log(res[1]);
            var uu=res[1].replace(regExp6,'.');
            //console.log(uu);
            //ip.push(uu);

            ip=ip+uu+'","';
        }
        //console.log(ip);
        while(res=regExp5.exec(body)){
            //console.log(res[1]);
            var uu=res[1].replace(regExp6,':');
            //console.log(uu);
            //mac.push(uu);
            mac=mac+uu+'","';
        }
        //console.log(mac);

        //var fanhui={"host":host,"ip":ip,"mac":mac};
        //console.log(typeof fanhui);
        var fanhui='{"host":['+host.slice(0,-2)+'],"ip":['+ip.slice(0,-2)+'],"mac":['+mac.slice(0,-2)+']}';


        //console.log(fanhui);
        quanju=fanhui;
        console.log(quanju);










        
        
        




        

        


       // console.log(arr3);







    }



    })



  }});








  

  }
});

console.log(quanju);